<?php
/**
 * | AndPHP框架[基于ThinkPHP5开发]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2017-2019 http://www.andphp.com
 * +----------------------------------------------------------------------
 * | AndPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
 * +----------------------------------------------------------------------
 * | author    :BabySeeME <417170808@qq.com>
 * +----------------------------------------------------------------------
 * | createTime :2018/3/13 001315:39
 * +----------------------------------------------------------------------
 */

namespace app\user\controller;


use app\common\controller\UserController;
use app\common\model\User as UserModel;
use app\user\validate\JoinValidate;

class Join extends UserController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

    }


    public function index(){
        $status = $this->config['user_join_on'];
        $join=true;
        if ($status!=1) {
           $join=false;
        }
        $this->assign('register',$join);

        return $this->fetch();
    }

    public function add(){
        $member = new UserModel();
        if (request()->isPost()) {
            $post = $this->request->post();

            $banned = $this->config['user_banned_title'];
            if (!empty($banned)) {
                $arr = explode(',', $banned);
                $post['nickname']=strtolower($post['nickname']);
                foreach ($arr as $k => $v) {
                    if(strtolower($v)==$post['nickname'] ){
                        return json(array('code' => 0, 'msg' => '你的昵称被禁止注册'));
                    }
                    if (strpos( $v, '*') !== false) {
                        $arrAll = explode('*', $v);
                        if (strpos($post['nickname'], strtolower($arrAll[1])) !== false) {
                            return json(array('code' => 0, 'msg' => '你的昵称包含【'.$arrAll[1].'】被禁止注册!'));
                        }
                    }
                }
            }
            //验证部分数据合法性
            $validate=(new JoinValidate())->goCheck($post);
           if($validate!==true){
               return json(array('code' => 0, 'msg' => $validate));
           }
            if (!captcha_check(input('post.captcha'))) {
                return json(array('code' => 0, 'msg' => '亲！验证码错误了哦'));
            }
            $set_nickAndSalt=$this->set_saltByNick($post['nickname']);
            $data['salt']=$set_nickAndSalt['salt'];
            $data['nickname']=$set_nickAndSalt['nickname'];
            $data['username']=config('database.prefix').$set_nickAndSalt['salt'].rand(10000,99999);
            $data['create_ip']=$this->request->ip();
            $data['password'] = passwordMD5($post['password'],$data['salt']);
            $data['email'] = $post['email'];
            $data['address'] =explode(',',$this->get_ip_address())[0];
            $data['phone'] = $post['phone'];
            $data['user_role_id'] = $this->config['user_role_default'];
            $count = [
                'point'=>explode(',',$this->config['point'])[3],
                'money'=>explode(',',$this->config['money'])[3],
            ];
            $returnID=$member->allowField(true)->together(['count'=>$count])->data($data)->save();
            if (!$returnID) {
                return json(array('code' => 0, 'msg' => '注册失败'));
                //$this->add_log(0, $data['username'], $data['nickname'].'注册于'.date('Y-m-d H:i:s',time()));

            }
            $findID = $member->where(['email'=>$data['email'],'salt'=>$data['salt']])->value('id');
            $member->saveRolesByID($findID,[$data['user_role_id']]);
            return json(array('code' => 200, 'msg' => '注册成功'));
        }
        return json(array('code' => 0, 'msg' => '非法提交'));
    }
    /**
     * 根据昵称获取加盐值 保证昵称数据唯一性
     * @param $nickname
     * @return mixed
     * @company    :WuYuZhong Co. Ltd
     * @author     :BabySeeME <417170808@qq.com>
     * @createTime :2018-03-13 16:30
     */
    public function set_saltByNick($nickname){
        $return['salt']=getRandStr(6);
        $return['nickname']=$nickname.'&'.$return['salt'];
        //实例化管理员模型
        $model =new UserModel();
        //验证昵称是否存在
        $nickname = $model->where('nickname',$return['nickname'])->select();
        if(!$nickname->isEmpty()) {
            $this->set_saltByNick($nickname);
        }
        return $return;
    }
}